﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HWmanagementDB
{
    public class Repository : IRepository, IDisposable
    {
        HWorkEntities hw = new HWorkEntities();

        GenericRepository<LOG> logrepository;
        public GenericRepository<LOG> LogRepository { get { if (logrepository == null) logrepository = new GenericRepository<LOG>(hw); return logrepository; } }

        GenericRepository<MESSAGE> messagerepository;
        public GenericRepository<MESSAGE> MessageRepository { get { if (messagerepository == null) messagerepository = new GenericRepository<MESSAGE>(hw); return messagerepository; } }

        GenericRepository<GROUP> grouprepository;
        public GenericRepository<GROUP> GroupRepository { get { if (grouprepository == null) grouprepository = new GenericRepository<GROUP>(hw); return grouprepository; } }

        GenericRepository<SUBJECT> subjectrepository;
        public GenericRepository<SUBJECT> SubjectRepository { get { if (subjectrepository == null) subjectrepository = new GenericRepository<SUBJECT>(hw); return subjectrepository; } }

        GenericRepository<USER> userrepository;
        public GenericRepository<USER> UserRepository { get { if (userrepository == null) userrepository = new GenericRepository<USER>(hw); return userrepository; } }

        GenericRepository<COURSE> courserepository;
        public GenericRepository<COURSE> CourseRepository { get { if (courserepository == null) courserepository = new GenericRepository<COURSE>(hw); return courserepository; } }

        GenericRepository<RESULT> resultrepository;
        public GenericRepository<RESULT> ResultRepository { get { if (resultrepository == null) resultrepository = new GenericRepository<RESULT>(hw); return resultrepository; } }

        GenericRepository<HOMEWORKDONE> homeworkdonerepository;
        public GenericRepository<HOMEWORKDONE> HomeworkdoneRepository { get { if (homeworkdonerepository == null) homeworkdonerepository = new GenericRepository<HOMEWORKDONE>(hw); return homeworkdonerepository; } }

        GenericRepository<HOMEWORK> homeworkrepository;
        public GenericRepository<HOMEWORK> HomeworkRepository { get { if (homeworkrepository == null) homeworkrepository = new GenericRepository<HOMEWORK>(hw); return homeworkrepository; } }

        GenericRepository<FILE> filerepository;
        public GenericRepository<FILE> FileRepository { get { if (filerepository == null) filerepository = new GenericRepository<FILE>(hw); return filerepository; } }

        public void Save()
        {
            hw.SaveChanges();
        }

        public void Dispose()
        {

            //throws exception if there is an error writing the database
            hw.SaveChanges();
            hw.Dispose();
        }
    }
}
